package com.reremouse.rerebbs.dao.impl;

import com.reremouse.rerebbs.dao.IBbsMessageDAO;
import com.reremouse.rerebbs.model.BbsMessage;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.io.Serializable;
import java.util.List;

public class BbsMessageDAOImpl
        implements IBbsMessageDAO, Serializable {
    @PersistenceContext
    private EntityManager entityManager;

    public EntityManager getEntityManager() {
        return this.entityManager;
    }

    public void setEntityManager(EntityManager entityManager) {
        this.entityManager = entityManager;
    }

    public void addMessageModel(BbsMessage ca) {
        this.entityManager.persist(ca);
    }

    public void deleteMessageModel(long id) {
        BbsMessage p = (BbsMessage) this.entityManager.find(BbsMessage.class, Long.valueOf(id));
        this.entityManager.remove(p);
    }

    public List<BbsMessage> findAllMessageModel() {
        String q = "Select ms from BbsMessage ms order by ms.genTime desc";
        List<BbsMessage> ais = this.entityManager.createQuery(q).getResultList();
        return ais;
    }

    public BbsMessage findMessageModel(long id) {
        BbsMessage c = (BbsMessage) this.entityManager.find(BbsMessage.class, Long.valueOf(id));
        return c;
    }

    public List<BbsMessage> findMessageModelByReceiver(long id) {
        String q = "Select ms from BbsMessage ms where ms.receiver.id=" + id + " and ms.receiverMarkDel=false order by ms.genTime desc";

        List<BbsMessage> ais = this.entityManager.createQuery(q).getResultList();
        return ais;
    }

    public List<BbsMessage> findUnReadedMsgByReceiver(long id) {
        String q = "Select ms from BbsMessage ms where ms.receiver.id=" + id + " and ms.receiverMarkDel=false and ms.readed=false order by ms.genTime desc";

        List<BbsMessage> ais = this.entityManager.createQuery(q).getResultList();
        return ais;
    }

    public long getUnReadedMsgNumByReceiver(long id) {
        String q = "Select count(ms) from BbsMessage ms where ms.receiver.id=" + id + " and ms.receiverMarkDel=false and ms.readed=false";

        long num = ((Long) this.entityManager.createQuery(q).getResultList().get(0)).longValue();
        return num;
    }

    public List<BbsMessage> findMessageModelBySender(long id) {
        String q = "Select ms from BbsMessage ms where ms.sender.id=" + id + " and ms.senderMarkDel=false order by ms.genTime desc";

        List<BbsMessage> ais = this.entityManager.createQuery(q).getResultList();
        return ais;
    }

    public void updateMessageModel(BbsMessage ca) {
        this.entityManager.merge(ca);
    }

    public void updateMessageReaded(long id) {
        BbsMessage m = (BbsMessage) this.entityManager.find(BbsMessage.class, Long.valueOf(id));
        m.setReaded(true);
        this.entityManager.merge(m);
    }

    public void receiverMarkDel(long id) {
        BbsMessage m = (BbsMessage) this.entityManager.find(BbsMessage.class, Long.valueOf(id));
        m.setReceiverMarkDel(true);
        this.entityManager.merge(m);
/* :0:   */
    }

    /* :1:   */
/* :2:   */
    public void senderMarkDel(long id)
/* :3:   */ {
/* :4:92 */
        BbsMessage m = (BbsMessage) this.entityManager.find(BbsMessage.class, Long.valueOf(id));
/* :5:93 */
        m.setSenderMarkDel(true);
/* :6:94 */
        this.entityManager.merge(m);
/* :7:   */
    }
/* :8:   */
}



/* Location:           F:\jsp源码\蝙蝠论坛系统\rerebbs\WEB-INF\classes\

 * Qualified Name:     com.reremouse.rerebbs.dao.impl.BbsMessageDAOImpl

 * JD-Core Version:    0.7.0.1

 */